<template>
  <div style="width: 100%">
    <a-card class="card" :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}">
              <a-form-item label="院系">
                <a-select :disabled="selectMenu.yx.length === 1" :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.ssyxm" @change="yxChange">
                  <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{i.dwh+"-"+  i.dwmc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}">
              <a-form-item label="专业">
                <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.sszym">
                  <a-select-option v-for="i in selectMenu.zy" :key="i.id" :value="i.zyh">{{i.zyh+"-"+ i.zymc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}">
                <a-form-item label="招生年度">
                  <a-select showSearch optionFilterProp="children" v-model="queryParam.zsxn">
                    <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}">
                <a-form-item label="学位层次">
                  <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.xwccm">
                    <a-select-option v-for="i in TYPE0119" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
            </template>
            <a-col :sm="{span:16}" :xl="{span:8}">
              <a-button type="primary" @click="loadData">查询</a-button>
              <a-button type="primary" @click="handleSave">保存</a-button>
              <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'" />
              </a>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-alert banner message="温馨提示：本页面显示的研究方向为上报后招生研究方向的汇总数据。" style="margin-bottom:20px"/>
    <a-card class="card" :bordered="false">
      <a-table ref="table" size="default" rowKey="key" :columns="columns" :loading="loading" :dataSource="data"
        :pagination="false">
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <template slot="action" slot-scope="text,record">
          <a-input style='max-width: 100px;' :key="record.id" :defaultValue='text' @change='changeValue(arguments[0],record.id)' />
        </template>
      </a-table>
    </a-card>
  </div>
</template>

<script>
  // import moment from 'moment'
  import {STable} from '~'
  import {mapState} from 'vuex'

  const selectArr = ['TYPE0126', 'TYPE0075', 'TYPE0125', 'TYPE0072','TYPE0119']

  export default {
    components: {
      STable
    },
    data() {
      return {
        advanced: true,
        AuditHandleVisible: false,
        loading: false,
        data: [],
        // 查询参数
        queryParam: {
          pyccm: '',
          ssyxm: '',
          sszym: '',
          zsxn: ''
        },
        postParams: {},
        // 表头
        columns: [{
            title: '序号',
            scopedSlots: {
              customRender: 'serial'
            }
          },
          {
            title: '研究方向码',
            dataIndex: 'yjfxm'
          },
          {
            title: '研究方向名称',
            dataIndex: 'yjfxmc'
          },
          {
            title: '所属院系',
            dataIndex: 'dwmc'
          },
          {
            title: '所属专业',
            dataIndex: 'zymc'
          },
          {
            title: '学位层次',
            dataIndex: 'pyccm'
          },
          {
            title: '研究方向序列',
            dataIndex: 'yjfxxl',
            scopedSlots: {
              customRender: 'action'
            }
          }
        ],
        selectMenu: {
          yx: [], // 院系
          zy: [], //专业
          xxcc: [] //学习层次
        },
        recruitEditData: {},
        pagination: {
          current: 1
        }
      }
    },
    computed: {
      ...mapState({
        systemInfo: state => state.app.systemInfo,
        TYPE0126: state => state.app['TYPE0126'], // 年级
        TYPE0119: state => state.app['TYPE0119']
      })
    },
    created() {
      this.initSelectMenu()

      selectArr.forEach(v => {
        this.$store.dispatch('app/setDictionary', v)
      })
    },
    methods: {
      initSelectMenu() {
        let allRequest = [
          this.$api.base.collegeAll()
        ]
        Promise.all(allRequest).then(res => {
          this.selectMenu = {
            yx: res[0].data
          }
          this.queryParam.ssyxm = res[0].data[0].dwh
           this.getProssion(this.queryParam.ssyxm)
          this.$api.base.dictionaryData('TYPE0119').then(res => {
            this.$set(this.selectMenu, 'xxcc', res.data)
          })
          // 获取当前学年
          this.$api.base.getSchoolYear().then(res => {
            this.queryParam.zsxn = res.data.masterAdmissionYear
            this.queryParam.xwccm = '2'    // 默认为硕士学位
            this.loadData({
              pageSize: 10,
              pageNum: 1
            })
          })
        })
      },
      loadData(parameter = {}) {
        if(!parameter.pageNum){
          this.pagination.current = 1
          parameter.pageNum = 1
        }
        // this.data = []
        let params = Object.assign(this.queryParam, parameter)
        this.loading = true
        this.$api.recruitstat.researchDirMainList(this.queryParam).then(res => {
          // console.log(res)

          const pagination = { ...this.pagination
          };
          pagination.total = 0
          if (res.data && res.data.total) {
            pagination.total = res.data.total;
          }
          this.data = []
          this.pagination = pagination;
          if (res.data && res.data.rows.length) {
             res.data.rows.map((item, index) => {
              this.selectMenu.xxcc.map((_item, _index) => {
                if (item.pyccm == _item.id) {
                  item.pyccmmc = _item.name
                }
              })
              return item
            })
            this.data = res.data.rows;
          }
          this.loading = false;
        })
      },
      // 分页
      handleTableChange(pagination, filters, sorter) {
        console.log(pagination);
        const pager = { ...this.pagination
        };
        pager.current = pagination.current;
        this.pagination = pager;
        this.loadData();
      },
      // 选择院系
      yxChange(code) {
        this.$set(this.queryParam, 'sszym', '')
        this.$set(this.selectMenu, 'zy', [])
        if(code){
          this.getProssion(code)
        }
      },
      getProssion(code) {
        this.$api.base.majorAll({
          lsdwh: code
        }).then(res => {
          this.$set(this.selectMenu, 'zy', res.data)
        })
      },
      //改变输入框的值
      changeValue(e, id) {
        console.log(e)
        var lastData = this.data
        lastData.map((item, index) => {
          if (item.id == id) {
            this.$set(this.data[index], 'yjfxxl', e.target.value)
          }
        })
      },
      // 保存
      handleSave(record) {
        // console.log('保存',JSON.stringify(this.data))
        this.$api.recruitstat.researchDirMainAdd({
          rultes: JSON.stringify(this.data)
        }).then(res => {
          this.loadData();
        })
      }
    }
  }
</script>

<style lang="less" scoped>
  .card {
    margin-bottom: 20px;
  }
</style>
